/**
 *  红包页面
 */

import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/not_found.dart';
import 'package:cbec_app/components/common/redpack_item.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/pages/my/controller/redpack_ctr.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

class RedPack extends StatelessWidget {
  RedPack({super.key});

  final RedPackController redPackCtr = Get.put(RedPackController());

  @override
  Widget build(BuildContext context) {
    // 获取屏幕高度、状态栏高度、底部安全区高度
    double screenHeight = MediaQuery.of(context).size.height;
    double statusBarHeight = MediaQuery.of(context).padding.top; // 状态栏高度
    double bottomPadding = MediaQuery.of(context).padding.bottom; // 底部安全区高度
    double appBarHeight = kToolbarHeight; // 导航栏高度
    double contentHeight = screenHeight - statusBarHeight - appBarHeight - bottomPadding - 10.w - 25;
    return BasePage(
      title: "我的红包",
      iconType: IconType.back,
      isShowPadding: true,
      paddingTop: 10.w,
      children: [
        //这里是上方的tabbar
        _topTabbar(),
        //红包容器
        Container(
            height: contentHeight,
            margin: EdgeInsets.only(top: 5.w),
            // color: Colors.red,
            child: Obx(() => redPackCtr.dataList.isNotEmpty
                ? Tools.getPullRefresh(
                    enablePullDown: true,
                    enablePullUp: redPackCtr.hasNextPage,
                    refreshController: redPackCtr.baseRefreshCtr,
                    onRefresh: redPackCtr.onPageRefresh,
                    onLoading: redPackCtr.onPageLoading,
                    child: ListView.builder(
                      itemCount: redPackCtr.dataList.length,
                      itemBuilder: (context, index) {
                        final item = redPackCtr.dataList[index];
                        return RedpackItem(
                          item: item,
                          refund: () {
                            Tools.showModal(
                              content: "确定要发起退款申请么?",
                              onConfirm: (tag) {
                                redPackCtr.refund(item['id']);
                                SmartDialog.dismiss(tag: tag);
                              },
                              onCancel: (tag) {
                                SmartDialog.dismiss(tag: tag);
                              },
                            );
                          },
                        );
                      },
                    ))
                : const NotFound())

            // ListView(
            //   children: [
            //     RedpackItem(price: "100", needMoney: "0", start_time: "2024.01.22", end_time: "2024.05.01", title: "无门槛", sub_title: "仅可购买部分商品", desc: "这里是使用说明",status: 1,status_text: "去使用",),
            //     RedpackItem(price: "100", needMoney: "0", start_time: "2024.01.22", end_time: "2024.05.01", title: "无门槛", sub_title: "仅可购买部分商品", desc: "这里是使用说明",status: 2,status_text: "已使用",),

            //   ],
            // ),
            ),
      ],
    );
  }

  Widget _topTabbar() => SizedBox(
      height: 25,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          TabBar(
              dividerColor: Colors.transparent,
              indicator: const BoxDecoration(
                border: Border(
                  bottom: BorderSide(width: 0.8, color: Colors.black),
                ),
              ),
              labelColor: Colors.black,
              unselectedLabelColor: const Color.fromRGBO(153, 153, 153, 1),
              controller: redPackCtr.tabController,
              isScrollable: true,
              padding: EdgeInsets.zero,
              labelPadding: EdgeInsets.only(right: 10.w),
              tabAlignment: TabAlignment.start,
              tabs: const [
                Tab(
                  text: "未使用",
                ),
                Tab(
                  text: "已使用",
                ),
                Tab(
                  text: "已过期",
                ),
              ]),

          //购券中心
          BaseInteractive(
            onClick: () {
              logger.d('前往购券中心');
              redPackCtr.toMarket();
            },
            childWidget: Row(
              children: [Text("前往购券中心", style: setTextStyle(12, FontWeight.w300, Tools.fromHex("#3D3D3D"))), Icon(Icons.chevron_right, size: 18, color: Tools.fromHex("#3D3D3D"))],
            ),
          )
        ],
      ));
}
